7 ROS2 -Creating ROS Package

ROS Package - CMAKE

  • CMakeLists.txt file that describes how to build the code within the package
  • include/<package_name> directory containing the public headers for the package
  • package.xml file containing meta information about the package
  • src directory containing the source code for the package

my_package/
CMakeLists.txt
include/my_package/
package.xml
src/

ROS Package - python

  • package.xml file containing meta information about the package
  • resource/<package_name> marker file for the package
  • setup.cfg is required when a package has executables, so ros2 run can find them
  • setup.py containing instructions for how to install the package
  • <package_name> - a directory with the same name as your package, used by ROS 2 tools to find your package, contains init.py

my_package/
package.xml
resource/my_package
setup.cfg
setup.py
my_package/

Packages in a workspace

workspace_folder/
src/
cpp_package_1/
CMakeLists.txt
include/cpp_package_1/
package.xml
src/

         
py_package_1/
package.xml
resource/py_package_1
setup.cfg
setup.py
py_package_1/
...

     
cpp_package_n/
CMakeLists.txt
include/cpp_package_n/
package.xml
src/

Create a package

  • The command syntax for creating a new package in ROS 2 is:

ros2 pkg create --build-type ament_python <package_name>

  • Open a terminal and run:
cs
ros2 pkg create --build-type ament_python --node-name my_node my_package

Pasted image 20251012093156.png

•In the same terminal and run:

cb

Pasted image 20251012093237.png

  • In the same terminal and run:
ros2 run my_package my_node

Pasted image 20251012093309.png

  • Check package
    Pasted image 20251012093323.png

  • Package.xml
    Pasted image 20251012093422.png

  • Setup.py
    Pasted image 20251012093517.png